﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0046)http://apps.hi.baidu.com/share/detail/22168746 --><HTML><HEAD><TITLE>转贴：iText html转pdf 百度空间_应用平台</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<META content=IE=7 http-equiv=X-UA-Compatible><LINK rel=stylesheet type=text/css 
href="1_files/appmerge.css"><LINK rel=stylesheet type=text/css 
href="1_files/share.css">
<META name=GENERATOR content="MSHTML 8.00.7600.16722"></HEAD>
<BODY>
<DIV id=appShareContent>
<DIV id=appShareTitle>
<DIV id=appShareShareIcon class=share-btn-img 
onclick="share.share('22168746','','22168746')"></DIV>iText html转pdf </DIV>
<DIV id=app-share-container>
<DIV id=appShareOpt></DIV>
<DIV id=app-share-content>
<P>package cn.it;</P>
<P>import java.io.FileOutputStream;<BR>import java.io.FileReader;<BR>import 
java.io.IOException;<BR>import java.util.ArrayList;</P>
<P>import com.lowagie.text.Document;<BR>import 
com.lowagie.text.DocumentException;<BR>import 
com.lowagie.text.Element;<BR>import com.lowagie.text.Font;<BR>import 
com.lowagie.text.PageSize;<BR>import com.lowagie.text.Paragraph;<BR>import 
com.lowagie.text.html.HtmlParser;<BR>import 
com.lowagie.text.html.simpleparser.HTMLWorker;<BR>import 
com.lowagie.text.html.simpleparser.StyleSheet;<BR>import 
com.lowagie.text.pdf.BaseFont;<BR>import com.lowagie.text.pdf.PdfWriter;</P>
<P></P>
<P>public class htmltopdf {<BR>public static void main(String[] args) {htmltopdf 
ih = new htmltopdf();&nbsp;&nbsp; <BR>&nbsp;&nbsp; 
ih.htmlCodeComeFromFile("D:\\xia\\main.html", "D:\\xia\\iText_3.pdf"); <BR>// 
ih.htmlCodeComeString("Hello中文", "D: <A 
href="file://test//iText_2.pdf">file://test//iText_2.pdf</A>");</P>
<P>}<BR>public void htmlCodeComeFromFile(String filePath, String pdfPath) 
{&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Document document = 
new Document();&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try 
{&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
StyleSheet st = new StyleSheet(); 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
st.loadTagStyle("body", "leading", "16,0"); 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
PdfWriter.getInstance(document, new FileOutputStream(pdfPath));&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
document.open();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", 
BaseFont.NOT_EMBEDDED);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Font 
FontChinese = new Font(bfChinese, 12, Font.NORMAL);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paragraph 
t = new Paragraph(filePath, FontChinese); 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList 
p = HTMLWorker.parseToList(new FileReader(filePath), st); 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int k 
= 0; k &lt; p.size(); ++k) {&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
document.add((Element)p.get(k));&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
document.close();&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.out.println("文档创建成功");&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(Exception e) {&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
e.printStackTrace();&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; } <BR></P>
<P>&nbsp;&nbsp;&nbsp; public void htmlCodeComeString(String htmlCode, String 
pdfPath) {&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Document 
doc = new Document(PageSize.A4);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
PdfWriter.getInstance(doc, new FileOutputStream(pdfPath));&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
doc.open();&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 
解决中文问题&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BaseFont 
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", 
BaseFont.NOT_EMBEDDED);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Font 
FontChinese = new Font(bfChinese, 12, Font.NORMAL);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Paragraph 
t = new Paragraph(htmlCode, FontChinese);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
doc.add(t);&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
doc.close();&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
System.out.println("文档创建成功");&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(Exception e) {&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
e.printStackTrace();&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
} <BR>&nbsp;&nbsp;&nbsp; } <BR>&nbsp;&nbsp;&nbsp;</P>
<P>}</P></DIV></DIV></DIV></BODY></HTML>
